package com.estate.web.controller.eastate;

import java.util.ArrayList;
import java.util.List;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.estate.common.utils.redis.RedisUtil;
import com.estate.system.domain.Introduction;
import com.estate.system.domain.IntroductionDetail;
import com.estate.system.service.IIntroductionDetailService;
import com.estate.system.service.IIntroductionService;
import com.estate.system.service.ISysAdvantageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.estate.common.annotation.Log;
import com.estate.common.enums.BusinessType;


import com.estate.common.core.controller.BaseController;
import com.estate.common.core.domain.AjaxResult;
import com.estate.common.utils.poi.ExcelUtil;
import com.estate.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 【请填写功能名称】Controller
 *
 * @author estate
 * @date 2022-04-08
 */
@Controller
@CrossOrigin
@RequestMapping("/system/introduction")

public class IntroductionController extends BaseController
{
    private String prefix = "system/introduction";

    @Autowired
    private IIntroductionService introductionService;
    @Autowired
    private IIntroductionDetailService introductionDetailService;
    @Autowired
    private ISysAdvantageService advantageService;
    @Resource
    RedisUtil redisUtil;

    @RequiresPermissions("system:introduction:view")
    @GetMapping()
    public String introduction()
    {
        return prefix + "/introduction";
    }

    /**
     *  【跳转到advantage页面】
     * **/
    @GetMapping("/advantage")
    public String toPost()
    {
        return "system/advantage/advantage";
    }

    /**
     * 查询【请填写功能名称】列表
     */

//    @PostMapping("/list")
//    @ResponseBody
//    public TableDataInfo list(Introduction introduction)
//    {
//
//        List<Introduction> list = introductionService.selectAllIntroductionList();
//        return getDataTable(list);
//    }


    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo listByPageHelper(Introduction introduction, int pageSize, int pageNum)
    {
        //如果这几个其中有一个都不为空则调用数据库的语句去查询
        System.out.println(introduction);
        if (!introduction.getTitle().equals("") || !introduction.getTypehouseArea().equals("") || introduction.getPrice() !=null
                || !introduction.getAdvantages().equals("") || introduction.getIfNeedShow()!=null
        ){
            startPage();
            List<Introduction> list = introductionService.selectIntroductionListByMapper(introduction);
            System.out.println("1");
            System.out.println(list);
            return getDataTable(list);


        }else {
            //混合pagehelper和redis的分页
            //但是不能像mysql一样做条件查询
            //使用redis分页,需要传参数过去
            System.out.println("2");
            PageInfo<Introduction> introductionPageInfo = introductionService.selectAllIntroductionList(pageSize, pageNum);
            introductionPageInfo.setPageSize(10);
            introductionPageInfo.setPageNum(1);
            System.out.println("list:"+introductionPageInfo);
            List<Introduction> list = introductionPageInfo.getList();
            TableDataInfo dataTable = getDataTable(list);
            dataTable.setTotal(introductionPageInfo.getTotal());
            System.out.println("introductionPageInfo.getTotal()"+introductionPageInfo.getTotal());


            return dataTable;
        }

    }
    /**
     * 直接分页
     * **/
    @PostMapping("/list/{pageSize}/{pageNum}")
    @ResponseBody
    public PageInfo<Introduction> listByPageHelper(@PathVariable int pageSize,@PathVariable int pageNum)
    {
        //混合pagehelper和redis的分页
        //但是不能像mysql一样做条件查询
        //使用redis分页,需要传参数过去
        System.out.println("2");
        PageInfo<Introduction> introductionPageInfo = introductionService.selectAllIntroductionList(pageSize, pageNum);
        introductionPageInfo.setPageSize(10);
        introductionPageInfo.setPageNum(1);

        return introductionPageInfo;


    }
    /**
     * 通过title字段模糊查询,并且分页
     *
     * **/
    @PostMapping("/list/title/{pageSize}/{pageNum}")
    @ResponseBody
    public PageInfo<Introduction> listByTitlePageHelper(@PathVariable int pageSize,@PathVariable int pageNum,String title)
    {

        Introduction introduction = new Introduction();
        introduction.setTitle(title);
        PageHelper.startPage(pageNum,pageSize);
        List<Introduction> list = introductionService.selectByTitleList(introduction);
        PageInfo<Introduction> pageInfo = new PageInfo<Introduction>(list);

        return pageInfo;
    }



    /**
     * 得到上传的图片地址并返回标签,1张图片
     */

    @PostMapping("/selectIntroductionImg")
    @ResponseBody
    public String afterUploadFileUpdateData(int introductionId)
    {
        Introduction introduction = introductionService.selectIntroductionById((long) introductionId);
        System.out.println("introduction119"+introduction);
        return introduction.getImgAddress();
    }
    /**
     * 得到advantage的值,便于回显优点标签数据
     */
    @PostMapping("/getAdvantageIntroduction")
    @ResponseBody
    public String getAdvantageIntroduction(int introductionId)
    {
        Introduction introduction = introductionService.selectIntroductionById((long) introductionId);

        return introduction.getAdvantages();
    }


    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("system:introduction:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Introduction introduction)
    {
        List<Introduction> list = introductionService.selectIntroductionList(introduction);
        ExcelUtil<Introduction> util = new ExcelUtil<Introduction>(Introduction.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("advantages", advantageService.selectAdvantageAll());
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("system:introduction:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Introduction introduction)
    {
        int res=introductionService.insertIntroduction(introduction);
        if (res ==1){
            return toAjax(true).put("id",introduction.getId());
        }
        return toAjax(false).put("id",0);
    }

    /**
     * 修改【请填写功能名称】
     */
    @RequiresPermissions("system:introduction:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Introduction introduction = introductionService.selectIntroductionById(id);
        IntroductionDetail introductionDetail = introductionDetailService.selectIntroductionDetailById(id);
        mmap.put("introduction", introduction);
        mmap.put("introductionDetail", introductionDetail);
        mmap.put("id",id);
        mmap.put("advantages", advantageService.selectAdvantageAll());
        return prefix + "/edit";
    }
    //    @RequiresPermissions("system:introduction:edit")
    @GetMapping("/edit_detail/{id}")
    public String edit_detail(@PathVariable("id") Long id, ModelMap mmap)
    {
        //根据id搜索,detail表的数据,不存在则返回null
        Introduction introduction = introductionService.selectIntroductionById(id);
        mmap.put("introduction", introduction);
        IntroductionDetail introductionDetail = introductionDetailService.selectIntroductionDetailById(id);
        mmap.put("introductionDetail", introductionDetail);
        mmap.put("advantages", advantageService.selectAdvantageAll());
        return prefix + "/edit_detail";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("system:introduction:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Introduction introduction,ModelMap mmap)
    {



        return toAjax(introductionService.updateIntroduction(introduction));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:introduction:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(introductionService.deleteIntroductionByIds(ids));
    }
}
